//
//  GridTreeNodeModel.swift
//  grid-ios-app
//
//  Created by wondertek on 2024/5/20.
//

import Foundation
import ObjectMapper


class GridTreeNodeModel: Mappable{
    required init?(map: Map) {}
    
    func mapping(map: Map) {
        companyName <- map["companyName"]
        children <- map["children"]
        id <- map["id"]
    }
    
    var id: String?
    var companyName: String?
    var children: [GridTreeNodeModel]?
    var isExpand: Bool = false // 是否合上
    var parent: GridTreeNodeModel?
    var level: Int = 1

    //是否为根节点
    func isRoot() -> Bool{
        return parent == nil
    }
    
    //判断父节点是否打开
    func isParentExpand() -> Bool {
        if parent == nil {
            return false
        }
        return (parent?.isExpand)!
    }
    
    //是否是叶子节点
    func isLeaf() -> Bool {
        if let childrens = children {
            return childrens.count == 0
        }
        return false
    }
    
    //设置展开
    func setExpand(_ isExpand: Bool) {
        self.isExpand = isExpand
        if !isExpand {
            if let childrens = children {
                for i in 0 ..< childrens.count {
                    childrens[i].setExpand(isExpand)
                }
            }
        }
    }
   
}
